package com.xxc.exam.service.impl

import com.alibaba.druid.sql.visitor.functions.Now
import com.xxc.batis.ext.startPage
import com.xxc.batis.qo.PageQO
import com.xxc.batis.service.CrudService
import com.xxc.batis.service.CrudServiceImpl
import com.xxc.common.const.NOW
import com.xxc.common.exceptions.DataConflictException
import com.xxc.exam.entity.dto.ContestDto
import com.xxc.exam.entity.pojo.Contest
import com.xxc.exam.entity.pojo.Paper
import com.xxc.exam.entity.pojo.SelectionQues
import com.xxc.exam.entity.vo.ContestVO
import com.xxc.exam.entity.vo.GetContextListVO
import com.xxc.exam.entity.vo.SelectionQuesVO
import com.xxc.exam.mapper.ContestMapper
import com.xxc.exam.mapper.PaperMapper
import com.xxc.exam.mapper.SelectionQuesMapper
import com.xxc.exam.service.ContestService
import org.springframework.beans.BeanUtils
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Propagation
import org.springframework.transaction.annotation.Transactional
import java.math.BigInteger
import java.sql.Time

@Service
class ContestServiceImpl:CrudServiceImpl<Contest,BigInteger,ContestMapper>(),ContestService {
    override fun getContestList(ownerId: BigInteger,pageQO: PageQO<*>): List<GetContextListVO> {
        //找到该管理员下的所有试卷
        val papers = paperMapper.select(Paper(ownerId = ownerId))
        if(papers.isEmpty())
            return emptyList()
        // 获取当前时间
        val now = NOW()
        startPage<Any,Any>(pageQO)
        return crudMapper.selectContestByPaperIds(papers.map { it.id!! }).map {// 转换为vo
            val vo = GetContextListVO()
            BeanUtils.copyProperties(it, vo)
            vo.sumScore = papers.find { paper ->
                it.paperId == paper.id
            }?.score
            vo
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    override fun getContest(id: BigInteger): ContestVO {
        val contest = crudMapper.selectByPrimaryKey(id)
        val now = NOW()
        if(contest.gmtStart!!.time > now.time || contest.gmtEnd!!.time < now.time)
        {
            throw DataConflictException("当前考试不存在")
        }

        val selections = selectionQuesMapper.select(SelectionQues(paperId = contest.paperId))
                .map {
                    val count = it.simpleCount!! + it.normalCount!! + it.complexCount!!
                    SelectionQuesVO(it.id, it.title, it.perScore!! * count,count)
                }
        return ContestVO(contest.timeLen,selections)
    }

    @Autowired
    lateinit var selectionQuesMapper: SelectionQuesMapper

    @Autowired
    lateinit var paperMapper: PaperMapper


    @Transactional(propagation= Propagation.SUPPORTS,readOnly=true)
    override fun addContest(dto: ContestDto,id: BigInteger) {
        val contest = Contest()
        val paper = Paper()
        val selections = ArrayList<SelectionQues>()

        paper.name = dto.name
        paper.score = dto.sumScore
        paper.ownerId = id
        paper.type = Paper.RANDOM_QUES
        paper.injectTime()

        paperMapper.insertSelective(paper)

        dto.ques!!.forEach {
            val sq = SelectionQues()
            sq.paperId = paper.id
            sq.categories = it.category
            sq.injectTime()
            BeanUtils.copyProperties(it,sq)
            selections.add(sq)
        }

        selectionQuesMapper.insertList(selections)

        BeanUtils.copyProperties(dto,contest)
        contest.timeLen = Time(dto.timeLen!!.time)
        contest.name = dto.name
        contest.ownerId = id
        contest.gmtStart = dto.startTime
        contest.gmtEnd = dto.endTime
        contest.injectTime()

        contest.paperId = paper.id
        crudMapper.insertSelective(contest)
    }
}